#include "Details.h"
#include "ui_Details.h"


Details::Details(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Details)
{
    ui->setupUi(this);
    group = new QButtonGroup;
    connect(group, QOverload<int>::of(&QButtonGroup::buttonClicked),[=](int id){
        // QLOG_LINE << id;
        ui->lb_ind->setText(group->button(id)->text());
        id = abs(id) - 2 ;
        ui->stackedWidget->setCurrentIndex(id);
    });
}

Details::~Details()
{
    delete ui;
}

void Details::setHasKey(DB_Keys m_keys)
{

    auto count = ui->stackedWidget->count();
    while (count == -1) {
        ui->stackedWidget->removeWidget(ui->stackedWidget->widget(count));
        count--;
    }

    for (auto ww:list) {
        ui->gridLayout->removeWidget(ww);
        group->removeButton(ww);
    }


    int indesx = 0;
    for (auto it= m_keys.begin();it!=m_keys.end();it++) {

        QPushButton *contorl = new QPushButton(it.key());
        QListView   *dataView = new QListView;
        QStringListModel *model = new QStringListModel;
        dataView->setModel(model);
        ui->gridLayout->addWidget(contorl);
        model->setStringList(*it);
        ui->stackedWidget->addWidget(dataView);

        list.push_back(contorl);
//        connect(contorl,&QPushButton::click,[this,&indesx](){
//            QLOG_LINE << indesx;
//           ui->stackedWidget->setCurrentIndex(indesx);
//        });
        group->addButton(contorl);
        indesx++;
    }
}
